 <!--
 @author: zgg (作者)
 @create: 2021-08-30 更改日期
   -->
<template>
  <div class="app-container">
    <!-- height="300"固定表头，border显示竖直边框 ,stripe显示斑马纹 -->
    <div class="admin-box">
      <div class="admin-title">管理员列表页面</div>
      <div class="search-box">
        <el-input v-model="realName" clearable class="s-name" style="margin-right:5px;" placeholder="用户真实姓名查询" @change="search" />
        <el-input v-model="name" clearable class="s-name" style="margin-right:10px;" placeholder="请输入账号查询" @change="search" />
        <el-button type="primary" icon="el-icon-search" style="margin: 15px 10px 0 0;" @click="search">查询/筛选</el-button>
        <el-button type="warning" icon="el-icon-circle-plus-outline" style="margin: 15px 0 0 0;" @click="addUser">添加用户</el-button>
      </div>
      <div class="admin-list-table-div">
        <!-- 前端分页模式将获取到的数据进行计算(后端不进行分页时) -->
        <el-table ref="table" v-loading="loading" :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="用户ID" width="180" />
          <el-table-column prop="username" label="账号" width="180" />
          <el-table-column prop="name" label="姓名" width="180" />
          <el-table-column prop="role_name" label="权限组" width="220" />
          <el-table-column label="状态" width="140">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.state==0" size="small" effect="dark">正常</el-tag>
              <el-tag v-if="scope.row.state==1" type="info" size="small" effect="light">禁用</el-tag>
            </template>
          </el-table-column>

          <!-- 自定义显示 -->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div v-if="scope.row.id!=1">
                <el-button plain size="small" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                <el-button plain size="small" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="admin-pagination-div">
        <el-pagination
          background
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :pager-count="pageCount"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { getUserList, deleteUser } from '@/api/admin-manage'

export default {
  name: 'Index',
  directives: { waves },
  data() {
    return {
      realName: '', // 真实姓名
      name: '', // 账号
      tableData: [], // 列表数据
      pageSize: 10, // 每页条数
      pageCount: 7, //
      pageSizes: [10, 20, 30, 40, 50, 100], // 个数选择器
      currentPage: 1, // 当前页码
      total: 0, // 总条数
      loading: false,
      labelPosition: 'right' // label对齐方式，可选值为right、top、left
    }
  },
  computed: {},
  mounted() {
    // this.getTableData()
    this.getData(this.currentPage, this.pageSize)
  },
  methods: {

    // 每页显示的条数
    handleSizeChange(val) {
      this.pageSize = val
      this.getData(1, val)
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1
    },

    // 显示第几页
    handleCurrentChange(val) {
      this.currentPage = val
      // 切换页码时，要获取每页显示的条数
      this.getData(val, this.pageSize)
    },

    // 获取管理员列表
    getData(page, size) {
      const param = {
        'admin.name': this.realName,
        'admin.username': this.name,
        'pageNumber': page,
        'pageSize': size
      }
      getUserList(param).then(res => {
        // console.log(res)
        if (res.state) {
          this.tableData = res.data.list // 数据
          this.total = res.data.totalRow // 总页码
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    // 搜索
    search() {
      this.getData(this.currentPage, this.pageSize)
    },

    // 添加用户
    addUser() {
      this.$router.push({ path: '/admin/adduser' })
    },

    // 编辑
    handleEdit(index, row) {
      // console.log(index, row)
      this.$router.push({ path: '/admin/upduser', query: { id: row.id }})
    },

    // 删除
    handleDelete(index, row) {
      // console.log(index, row)
      this.$confirm('你是否确定删除这条信息?', '询问', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delUser(row.id)
      }).catch(() => {})
    },

    // 用户删除
    delUser(id) {
      deleteUser({ id: id }).then(res => {
        if (res.state) {
          this.$message({ type: 'success', message: res.msg, duration: 2000 })
          this.getData(this.currentPage, this.pageSize)
        } else {
          this.$message.error(res.msg)
        }
        // console.log(res)
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.admin-box{
  border: 1px solid #f2f2f2;
  border-radius: 3px;
  box-shadow: 0 1px 1px 0 rgba(0,0,0,.12);
  background: #fff;

  .admin-title{
    font-size: 16px;
    color: #333;
    padding: 15px;
    border-bottom: 1px solid #f2f2f2;
  }

  .search-box{
    padding: 0 15px;

    .s-name{
      width: 200px;
      margin-top: 15px;
    }
  }

  .admin-list-table-div{
    padding: 15px;
  }

  .admin-pagination-div{
    display: flex;
    align-items: center;
    // justify-content: flex-end; //右对齐
    // margin-top: 10px;
    padding: 10px 15px;
    border-top: 1px solid #dfe6ec;
  }

}
</style>

<style lang="scss"> //element-ui样式修改写到这里面

</style>
